package office.service.sale.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.sale.form.ProductDetailConditionForm;
import office.action.sale.form.ProductDetailForm;
import office.action.sale.wrapper.ProductDetailWrapper;
import office.bo.sale.IProductDetail;
import office.bo.sale.impl.ProductDetail;
import office.bo.sale.impl.Rival;
import office.domainset.BizDomainProviderSetImpl;
import office.service.sale.IProductDetailService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.Util;

public class ProductDetailServiceImpl extends BaseBizServiceImpl implements IProductDetailService{
	
	public void editProductDetail(ProductDetailForm form)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			ProductDetail bo = (ProductDetail)domainProviderSetImpl.getProductDetailDP().newEmptyObj();
			bo =(ProductDetail) form.copyPropertiesToBO(bo);
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑产品明细失败!", e);
		}
	}
	
	public Page getPageInfo(ProductDetailConditionForm condition)throws BusinessException{
		Page page = new Page();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getProductDetailDP();
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "ProductDetail.pageSize"));
			page.setPageSize(pageSize);
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getProductName())){
				condMap.put("productName", condition.getProductName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider, "findProductDetailByIdAndName", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
			
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	public List<ProductDetailWrapper> findAllProductDetail(ProductDetailConditionForm condition,Page page)throws BusinessException{
		List<ProductDetailWrapper> wrapperList = new ArrayList();
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getProductDetailDP();
			Map condMap= new HashMap();
			if(null!=condition&&!Util.isNull(condition.getProductName())){
				condMap.put("productName", condition.getProductName());
			}
			List list = domainProviderSetImpl.getDao().findByDynamicHql(provider,"findProductDetailByIdAndName",condMap,new HashMap(),page);
			ProductDetailWrapper wrapper;
			for(int i=0;i<list.size();i++){
				ProductDetail bo = (ProductDetail)list.get(i);
				Rival rival=bo.getRival(domainProviderSetImpl);
				bo.setRivalName(rival.getUnitName());
				wrapper = new ProductDetailWrapper();
				wrapper.copyToWrapper(bo);
				wrapperList.add(wrapper);
			}
		}catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取产品明细列表失败！", e);
		}
		return wrapperList;
	}

	public void delProductDetailById(String id)throws BusinessException{
		try{
			BizDomainProviderSetImpl domainProviderSetImpl=(BizDomainProviderSetImpl)domainProviderSet;
			IDomainProvider provider = domainProviderSetImpl.getProductDetailDP();
			domainProviderSetImpl.getDao().deleteById(provider, id);
		}catch(Exception e){
			log.error("", e);
			throw this.handleBusinessServiceException("删除产品明细失败！", e);
		}
	}
	
	public ProductDetailForm getById(String id)throws BusinessException{
		ProductDetailForm form = new ProductDetailForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			IProductDetail bo = (IProductDetail)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getProductDetailDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取产品明细失败!", e);
		}
		return form;
	}
}
